/**
 * 地图模块 - 监测预警
 */
export default {
    data() {
        return {
            legend: [
                // { name: '紫', icon: require('@/assets/map/village/0.png') },
                { name: '红', icon: require('@/assets/map/village/1.png'), color: '#d03319' },
                { name: '橙', icon: require('@/assets/map/village/2.png'), color: '#e18130' },
                { name: '黄', icon: require('@/assets/map/village/3.png'), color: '#efdb3d' },
                { name: '蓝', icon: require('@/assets/map/village/4.png'), color: '#061dd0' }
            ],
            mdName: '',
        }
    },
    // 计算属性
    computed: {
        // 流域编码
        basin() {
            return this.$store.getters.basin
        }
    },
    // 生命周期-创建
    created() {
        this.entitiesPonitYJJC = new Map()

        // 监听tabchange事件
        this.$eventBus.$on('EB_YJ_TAB_Change', res => {
            // console.log(res)
            this.mdName = res.mdName
            if (res.mdName === '监测预警') {
                this.showYJJC()
            } else {
                this.hideYJJC()
            }
        })
    },
    // 生命周期-销毁前
    beforeDestroy() {
    },
    // 生命周期-销毁
    destroyed() {
        // 清理
        this.clearYJJC()
        this.$store.getters.eventCesiumOption.leftPickFns.delete('YJJC')
        this.$eventBus.$off('EB_YJ_TAB_Change')
    },
    // 生命周期-挂载
    mounted() {
    },
    methods: {
        /**
         * 模块构建
         * @param {object} params 请求参数信息
         */
        creatYJJC(params, villageAdcd) {
            console.log('模块构建-YJJC')
            this.clearYJJC();
            if (this.mdName != '监测预警') {
                return;
            }
            var urlApi = '/api/EarlyWarning/GetMonitoringWarningAdcdList'
            return this.$api.Get(urlApi, params).then(res => {
                res.data.forEach(item => {
                    console.log(item);
                    var Village = item.Village;
                    var lv = Number(item.WLevel) - 1;
                    var color = this.legend[lv].color;
                    var entity = {
                        eventName: 'YJJC',
                        prop: item,
                        position: Cesium.Cartesian3.fromDegrees(
                            Number(item.Lgtd),
                            Number(item.Lttd)
                        ),
                        label: {
                            text: Village,
                            scale: 1,
                            font: '14px sans-serif',
                            fillColor: Cesium.Color.fromCssColorString(color),
                            outlineColor: Cesium.Color.fromCssColorString('#FFF'),
                            outlineWidth: 4,
                            style: Cesium.LabelStyle.FILL_AND_OUTLINE,
                            heightReference: Cesium.HeightReference.RELATIVE_TO_GROUND,
                            pixelOffset: new Cesium.Cartesian2(0, -20),
                            eyeOffset: new Cesium.Cartesian3(0, 0, 80),
                            horizontalOrigin: Cesium.HorizontalOrigin.CENTER, // 垂直
                            verticalOrigin: Cesium.VerticalOrigin.BOTTOM,
                            disableDepthTestDistance: 50000000,
                            distanceDisplayCondition: new Cesium.DistanceDisplayCondition(0.0, 200000)
                        },
                        billboard: {
                            image: this.legend[lv].icon,
                            horizontalOrigin: Cesium.HorizontalOrigin.CENTER,
                            verticalOrigin: Cesium.VerticalOrigin.BOTTOM,
                            heightReference: Cesium.HeightReference.CLAMP_TO_GROUND,
                            distanceDisplayCondition: new Cesium.DistanceDisplayCondition(0.0, this.$config.mapConfig.pb_far),
                            width: this.$config.mapConfig.billboardSize,
                            height: this.$config.mapConfig.billboardSize
                        },
                    }
                    this.entitiesPonitYJJC.set(item.Ewcode, this.$viewer.entities.add(entity))
                    // this._dataSourcePointYJJC.entities.add(entity)
                })
                // this.$viewer.dataSources.add(this._dataSourcePointYJJC)
                this.initEvevtYJJC()
            })
        },
        /**
         * 模块事件初始化
         */
        initEvevtYJJC() {
            var eventFn = pickedObject => {
                if (
                    pickedObject &&
                    pickedObject.id &&
                    pickedObject.id.eventName
                ) {
                    if (pickedObject.id.eventName === 'YJJC') {
                        this.flyToVillShowPopYJJC(pickedObject.id.prop.Ewcode)
                    }
                }
            }
            this.$store.getters.eventCesiumOption.leftPickFns.set('YJJC', eventFn)
        },
        flyToVillShowPopYJJC(adcd) {
            var entity = this.entitiesPonitYJJC.get(adcd)
            if (entity) {
                this.$viewer.flyTo(entity, {
                    duration: 0.5,
                    offset: {
                        heading: this.$viewer.camera.heading,
                        pitch: this.$viewer.camera.pitch,
                        range: 5000
                    }
                })
            }
            const popStayParams = this.$store.getters.popStayParams
            entity.prop.eventName = 'YJJC'
            entity.prop.rainStart = this.ruleForm.dtBegin
            entity.prop.rainEnd = this.ruleForm.dtEnd
            popStayParams.name = '村庄预警信息'
            // popStayParams.posX = Math.round(event.position.x)
            // popStayParams.posY = Math.round(event.position.y)
            popStayParams.remarks = entity.prop
            popStayParams.remarks.tabel = '监测预警'
            this.$store.dispatch('popStayParams', popStayParams)
        },
        /**
         * 模块清理
         */
        clearYJJC() {
            if (this.entitiesPonitYJJC && this.entitiesPonitYJJC.size > 0) {
                this.entitiesPonitYJJC.forEach((val, key, map) => {
                    this.$viewer.entities.remove(val)
                    map.delete(key)
                })
                // this.entitiesPonitYJJC = new Map()
            }
            this.$store.dispatch('closePop')
            this.$store.dispatch('closePopStay')
            // if (this._dataSourcePointYJJC &&
            //     this._dataSourcePointYJJC.entities &&
            //     this._dataSourcePointYJJC.entities.removeAll
            // ) {
            //     this._dataSourcePointYJJC.entities.removeAll()
            // }
            if (this.screenSpaceEventHandlerYJJC) {
                this.screenSpaceEventHandlerYJJC.destroy()
                this.screenSpaceEventHandlerYJJC = null
            }
        },
        /**
         * 显示模块
         */
        showYJJC() {
            if (this.entitiesPonitYJJC && this.entitiesPonitYJJC.size > 0) {
                this.entitiesPonitYJJC.forEach((val, key, map) => {
                    val.show = !0
                })
            }
        },
        /**
         * 隐藏模块
         */
        hideYJJC() {
            if (this.entitiesPonitYJJC && this.entitiesPonitYJJC.size > 0) {
                this.entitiesPonitYJJC.forEach((val, key, map) => {
                    val.show = !1
                })
            }
        }
    }
}
